<?php

header("Content-type: application/xhtml+xml");

require_once('../lib/Smarty/Smarty.class.php');
require_once('../lib/HimaShare.php');
require_once('../lib/Schedule.php');
require_once('../lib/Util.php');
require_once('../lib/lib.mobile.php');
require_once('../lib/Config.php');
require_once('../lib/Note.php');
require_once('../lib/Note/List.php');
require_once('../lib/MyList.php');

$data   = make_page(array_merge($_GET, $_POST));
$smarty = new Smarty();
$smarty->assign($data);
$smarty->display($data['error_code'] ? 'error.tmpl' : 'view_schedule.tmpl');

function make_page ($params) {
   $data = array('error_code' => 0);

    if (!validate_signature($params)) {
        $data['error_code'] = 1;
        return $data;
    }

    $date   = $params['date'];
    $user   = $params['opensocial_owner_id'];
    $app_id = $params['opensocial_app_id'];

    if (isset($params['note_id'])) {
        $note = new Note($params['note_id']);

        if (isset($params['set_flg'])) {
            $note->set_date($date);
        }

        else if (isset($params['unset_flg'])) {
            $note->set_date(0);
        }

        else if(isset($params['change_status'])){
            if ($note->is_done())
                $note->set_undone();
            else
                $note->set_done();
        }

        else if(isset($params['change_share_flg'])){
            if ($note->is_public())
                $note->set_private();
            else
                $note->set_public();
        }
    }

    $list = new MyList($user);
    $own_schedules    = $list->get_list_of_date_self($date);
    $friend_schedules = $list->get_list_of_date_friends($date);
    $note_select_list = Note_List::get_by_member($user);

    //asign
    $data['app_id']           = $app_id;
    $data['own_schedules']    = $own_schedules;
    $data['friend_schedules'] = $friend_schedules;
    $data['date']             = date_to_unix($date);
    $data['date_link']        = $date;
    $data['note_select_list'] = $note_select_list;
    $data['url_prefix']       = URL_PREFIX;

    $data['analytics']    = '<img src="' . googleAnalyticsGetImageUrl() . '" />'; // for google analytics

    return $data;


}

?>
